//
//  TBNewFeatureViewController.swift
//  新浪微博
//
//  Created by bin on 16/1/28.
//  Copyright © 2016年 XMG. All rights reserved.
//

import UIKit
import SnapKit

class TBNewFeatureViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()
        setupCollectionView()
    }

    func setupCollectionView(){
        // 开启分页
        collectionView.pagingEnabled = true;
        // 隐藏滚动条
        collectionView.showsHorizontalScrollIndicator = false;
        // 取消弹簧效果
        collectionView.bounces = false;
    }
    
}

class TBCollectionViewLayout: UICollectionViewFlowLayout{
    override func prepareLayout() {
        super.prepareLayout()
        // 设置cell尺寸
        itemSize = UIScreen.mainScreen().bounds.size;
        // 设置最小行间距 10
        minimumLineSpacing = 0;
        // 设置每个cell之间间距
        minimumInteritemSpacing = 0;
        // 设置每一组的间距
        //    layout.sectionInset = UIEdgeInsetsMake(100, 0, 0, 0);
        // 设置滚动方向
        scrollDirection = UICollectionViewScrollDirection.Horizontal;
        
    }
}

class TBCollectionViewCell:UICollectionViewCell{

    var index: Int = 0{
        didSet{
            let imageName = "new_feature_\(index+1)"
            iconView.image = UIImage(named: imageName)
        }
    }
    
    private lazy var iconView: UIImageView = {
        let iconImageView = UIImageView()
        return iconImageView
    }()
    
    private lazy var loginBtn: UIButton = {
        let loginBtn = UIButton(backgroundImage: "new_feature_button")
        loginBtn.hidden = true
        loginBtn.addTarget(self, action: Selector("loginBtnClicked"), forControlEvents: UIControlEvents.TouchUpInside)
        return loginBtn
    }()
    
    @objc private func loginBtnClicked(){
        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewControllerNotification, object: self, userInfo: ["vc" : "main"])
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupIconView()
    }
    func setupIconView(){
        
        contentView.addSubview(iconView)
        iconView.snp_makeConstraints(closure: { (make) -> Void in
            make.top.left.right.bottom.equalTo(0)
        })
        contentView.addSubview(loginBtn)
        loginBtn.snp_makeConstraints(closure: { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        })
        
    }
}

extension TBNewFeatureViewController: UICollectionViewDataSource , UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4;
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("NewFeatureCell", forIndexPath: indexPath) as! TBCollectionViewCell
//        cell.backgroundColor = UIColor.init(colorLiteralRed: Float(Float(random())/Float(RAND_MAX)), green: Float(Float(random())/Float(RAND_MAX)), blue: Float(Float(random())/Float(RAND_MAX)), alpha: 1)
        cell.index = indexPath.item
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let index = collectionView.indexPathsForVisibleItems().last!
        let cell = collectionView.cellForItemAtIndexPath(index) as! TBCollectionViewCell
        if index.item == 3 {
            cell.loginBtn.hidden = false
            cell.loginBtn.userInteractionEnabled = false
            cell.loginBtn.transform = CGAffineTransformMakeScale(0, 0)
            UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                cell.loginBtn.transform = CGAffineTransformIdentity
                }, completion: { (finished) -> Void in
                    cell.loginBtn.userInteractionEnabled = true
            })
        }else{
             cell.loginBtn.hidden = true
        }
        print("\(index)")
    }
}
